package com.dotstone.chipism.ui.view;



import android.app.Activity;
import android.content.Context;
import android.graphics.drawable.ColorDrawable;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.view.View.OnClickListener;
import android.view.ViewGroup.LayoutParams;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
import android.widget.Toast;

import com.dotstone.chipism.R;
import com.dotstone.chipism.ui.timing.RemoteSocketTimingActivity;

public class DevicePopWindow extends PopupWindow implements OnClickListener {
	private View contentView1;
	private LinearLayout updateLl;
	private LinearLayout deleteLl;
	private Context context;
	private int deviceid;
	private String devicename;
	private String groupname;
	private int position;
	private int h;
	private int w;

	public DevicePopWindow(final Activity contextx, View parent, int position) {
		this.context = contextx;
		this.deviceid = deviceid;
		this.devicename = devicename;
		this.groupname = groupname;
		this.position = position;
		LayoutInflater mLayoutInflater = LayoutInflater.from(context);
		contentView1 = mLayoutInflater
				.inflate(R.layout.device_popuwindow, null);
		h = contextx.getWindowManager().getDefaultDisplay().getHeight();
		w = contextx.getWindowManager().getDefaultDisplay().getWidth();
		// 设置SelectPicPopupWindow的View
		this.setContentView(contentView1);
		// 设置SelectPicPopupWindow弹出窗体的宽
		this.setWidth(w / 2 + 150);
		// 设置SelectPicPopupWindow弹出窗体的高
		this.setHeight(LayoutParams.WRAP_CONTENT);
		// 设置SelectPicPopupWindow弹出窗体可点击
		this.setFocusable(true);
		this.setOutsideTouchable(true);
		// 实例化一个ColorDrawable颜色为半透明
		ColorDrawable dw = new ColorDrawable(0x00000000);
		// 点back键和其他地方使其消失,设置了这个才能触发OnDismisslistener ，设置其他控件变化等操作
		this.setBackgroundDrawable(dw);
		// 产生背景变暗效果
		WindowManager.LayoutParams lp = contextx.getWindow().getAttributes();
		lp.alpha = 0.8f;
		contextx.getWindow().setAttributes(lp);
		// 刷新状态
		this.update();
		// 以下拉方式显示popupwindow
		// popuWindow1.showAsDropDown(parent, 180, 180);
		this.setOnDismissListener(new OnDismissListener() {

			// 在dismiss中恢复透明度
			public void onDismiss() {
				WindowManager.LayoutParams lp = contextx.getWindow()
						.getAttributes();
				lp.alpha = 1f;
				contextx.getWindow().setAttributes(lp);
			}
		});
		updateLl = (LinearLayout) contentView1.findViewById(R.id.updateLl);
		deleteLl = (LinearLayout) contentView1.findViewById(R.id.deleteLl);
		updateLl.setOnClickListener(this);
		deleteLl.setOnClickListener(this);

	}

	/**
	 * 显示popupWindow
	 * 
	 * @param parent
	 */
	public void showPopupWindow(View parent) {
		if (!this.isShowing()) {
			// 显示在左上方
			this.showAtLocation(parent, Gravity.TOP, 0,
					(int) (0.42* h));
			// 以下拉方式显示popupwindow
			this.showAsDropDown(parent, parent.getLayoutParams().width / 2, 180);
		} else {
			this.dismiss();
		}
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.updateLl:

			break;
		case R.id.deleteLl:
			try {
					// 删除后再把分组内devnum减1
			     	RemoteSocketTimingActivity.instance.deleteItem(position);
					DevicePopWindow.this.dismiss();

			} catch (Exception e) {
				e.printStackTrace();
			}

			break;
		default:
			break;
		}

	}

}